package com.jwell.panprovider.ui.activity

import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.view.View
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panprovider.R
import com.jwell.panprovider.contract.WaybillInfoContract
import com.jwell.panprovider.entity.WaybillInfoBean
import com.jwell.panprovider.presenter.WaybillInfoPresenter
import com.jwell.panprovider.ui.adapter.ViewpagerAdapter
import com.jwell.panprovider.ui.fragment.waybillinfo.BasicInfoFragment
import com.jwell.panprovider.ui.fragment.waybillinfo.CheckInfoFragment
import kotlinx.android.synthetic.main.activity_waybill_info.*
import kotlinx.android.synthetic.main.error_layout.*


/**
 * 运单详情
 */
class WaybillInfoActivity : MVPBaseActivity<WaybillInfoPresenter>(), WaybillInfoContract.View {

    override fun createPresenter(): WaybillInfoPresenter? {
        return WaybillInfoPresenter()
    }

    private val tabTitles = mutableListOf("基本信息", "进厂验收信息")
    private var fragments = mutableListOf(
            BasicInfoFragment(),
            CheckInfoFragment())
    private var listener: DataListener? = null
    private var listener1: DataListener1? = null
    private var id = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_waybill_info)
        initToolBar("运单详情", true, null)
        intent?.let {
            id = it.getIntExtra("id", 0)
        }
        presenter?.getData(id)

    }

    override fun showData(data: WaybillInfoBean) {
        when (data.billState) {
            1 -> {//运输中
                ll_bottom.visibility = View.VISIBLE
                text_canCell.visibility = View.VISIBLE
                ll_1.visibility = View.VISIBLE
            }
            2 -> {//正在排号
                ll_bottom.visibility = View.GONE
            }
            3 -> {//缓冲区等待
                ll_bottom.visibility = View.GONE
            }
            4 -> {//入库中
                ll_bottom.visibility = View.GONE
            }
            5 -> {//已收料
                ll_bottom.visibility = View.GONE
            }
            6 -> {//排号失效
                ll_bottom.visibility = View.GONE
            }
            7 -> {//超时未送达
                ll_bottom.visibility = View.VISIBLE
                text_canCell.visibility = View.VISIBLE
                ll_1.visibility = View.VISIBLE
                ll_2.visibility = View.VISIBLE
                text_delay.visibility = View.GONE
            }
            8 -> {//申请延期
                ll_bottom.visibility = View.GONE
            }
            9 -> {//已作废
                ll_bottom.visibility = View.GONE
            }
        }
        frame_content.visibility = View.VISIBLE
        errorLayout.visibility = View.GONE
        viewPager.offscreenPageLimit = 2
        viewPager.adapter = ViewpagerAdapter(fm = supportFragmentManager, fragmentList = fragments, tabTitles = tabTitles)
        tablayout.setupWithViewPager(viewPager)
        listener?.setData(data)
        listener1?.setData(data)
        //撤销
        text_canCell.setOnClickListener {
            showDialog("是否撤销?", DialogInterface.OnClickListener { dialog, which ->
                dialog?.dismiss()
                presenter?.revokeWaybill(id)
            }, DialogInterface.OnClickListener { dialog, which -> dialog?.dismiss() })

        }
        //申请延期
        text_delay.setOnClickListener {
            showDialog("申请延期需要后台审核，可延期的时长由后台审核时确定，若审核不通过，该运单会直接被作废！确认申请延期？", DialogInterface.OnClickListener { dialog, which ->
                dialog?.dismiss()
                presenter?.applyDelay(id)
            }, DialogInterface.OnClickListener { dialog, which -> dialog?.dismiss() })
        }
    }

    override fun showDataFailed(str: String) {
        frame_content.visibility = View.GONE
        errorLayout.visibility = View.VISIBLE
        ll_bottom.visibility = View.GONE
        ll_error.setOnClickListener {
            presenter?.getData(id)
        }
    }

    interface DataListener {
        fun setData(data: WaybillInfoBean)
    }

    interface DataListener1 {
        fun setData(data: WaybillInfoBean)
    }

    fun setListener(listener: DataListener) {
        this.listener = listener
    }

    fun setListener1(listener1: DataListener1) {
        this.listener1 = listener1
    }
}
